package com.xxl.job.admin.controller;

import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum;
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.admin.service.impl.XxlJobServiceImpl;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
import com.xxl.job.core.glue.GlueTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * index controller
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
@RequestMapping("/jobinfo")
public class JobInfoController {

	private static Logger logger = LoggerFactory.getLogger(JobInfoController.class);

	@Resource
	private XxlJobGroupDao xxlJobGroupDao;
	@Resource
	private XxlJobService xxlJobService;
	
	@RequestMapping
	public String index(Model model, @RequestParam(required = false, defaultValue = "-1") int jobGroup) {

		// 枚举-字典
		model.addAttribute("ExecutorRouteStrategyEnum", ExecutorRouteStrategyEnum.values());	// 路由策略-列表
		model.addAttribute("GlueTypeEnum", GlueTypeEnum.values());								// Glue类型-字典
		model.addAttribute("ExecutorBlockStrategyEnum", ExecutorBlockStrategyEnum.values());	// 阻塞处理策略-字典

		// 任务组
		List<XxlJobGroup> jobGroupList =  xxlJobGroupDao.findAll();
		model.addAttribute("JobGroupList", jobGroupList);
		model.addAttribute("jobGroup", jobGroup);

		return "jobinfo/jobinfo.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,
			int jobGroup, String jobDesc, String executorHandler, String filterTime) {
		
		return xxlJobService.pageList(start, length, jobGroup, jobDesc, executorHandler, filterTime);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> add(XxlJobInfo jobInfo) {
		return xxlJobService.add(jobInfo);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> update(XxlJobInfo jobInfo) {
		return xxlJobService.update(jobInfo);
	}

	@RequestMapping("/updateCron")
	@ResponseBody
	public ReturnT<String> updateCron(XxlJobInfo jobInfo) {

		ReturnT<XxlJobInfo> xxlJobInfoReturnT = xxlJobService.get(jobInfo.getId());
		if(xxlJobInfoReturnT.getContent() != null) {

			XxlJobInfo _jobInfo = xxlJobInfoReturnT.getContent();

			if(jobInfo.getJobCron() != null && !jobInfo.getJobCron().equals("")) {
				_jobInfo.setJobCron(jobInfo.getJobCron());
			}

			if(jobInfo.getJobDesc() != null && !jobInfo.getJobDesc().equals("")) {
				_jobInfo.setJobDesc(jobInfo.getJobDesc());
			}

			if(jobInfo.getExecutorParam() != null && !jobInfo.getExecutorParam().equals("")) {
				_jobInfo.setExecutorParam(jobInfo.getExecutorParam());
			}

			ReturnT<String> update = xxlJobService.update(_jobInfo);
			return update;
		} else {
			return ReturnT.FAIL;
		}
	}

	@RequestMapping("/updateCronByName")
	@ResponseBody
	public ReturnT<String> updateCronByName(XxlJobInfo jobInfo) {
		XxlJobInfo xxljob = xxlJobService.getByName(jobInfo.getJobDesc());
		if(xxljob != null) {
			jobInfo.setId(xxljob.getId());
		}
		return updateCron(jobInfo);
	}


	@RequestMapping("/remove")
	@ResponseBody
	public ReturnT<String> remove(int id) {
		return xxlJobService.remove(id);
	}

	@RequestMapping("/removeByName")
	@ResponseBody
	public ReturnT<String> removeByName(String name) {

		XxlJobInfo xxlJobInfo = xxlJobService.getByName(name);
		if(xxlJobInfo == null) {
			logger.warn("Failed to remove scheduled task = {}", name);
			return ReturnT.FAIL;
		}
		return xxlJobService.remove(xxlJobInfo.getId());
	}

	@RequestMapping("/get")
	@ResponseBody
	public ReturnT<XxlJobInfo> get(int id) {
		return xxlJobService.get(id);
	}

	@RequestMapping("/getByName")
	@ResponseBody
	public ReturnT<XxlJobInfo> getByName(String name) {
		return new ReturnT<>(xxlJobService.getByName(name));
	}

	@RequestMapping("/stop")
	@ResponseBody
	public ReturnT<String> pause(int id) {
		return xxlJobService.stop(id);
	}

	@RequestMapping("/stopByName")
	@ResponseBody
	public ReturnT<String> stopByName(String name) {
		XxlJobInfo xxlJobInfo = xxlJobService.getByName(name);
		if(xxlJobInfo == null) {
			logger.warn("Failed to stop scheduled task = {}", name);
			return ReturnT.FAIL;
		}
		return xxlJobService.stop(xxlJobInfo.getId());
	}
	
	@RequestMapping("/start")
	@ResponseBody
	public ReturnT<String> start(int id) {
		return xxlJobService.start(id);
	}

	@RequestMapping("/startByName")
	@ResponseBody
	public ReturnT<String> startByName(String name) {
		XxlJobInfo xxlJobInfo = xxlJobService.getByName(name);
		if(xxlJobInfo == null) {
			logger.warn("Failed to start scheduled task = {}", name);
			return ReturnT.FAIL;
		}
		return xxlJobService.start(xxlJobInfo.getId());
	}
	
	@RequestMapping("/trigger")
	@ResponseBody
	//@PermessionLimit(limit = false)
	public ReturnT<String> triggerJob(int id, String executorParam) {
		// force cover job param
		if (executorParam == null) {
			executorParam = "";
		}

		JobTriggerPoolHelper.trigger(id, TriggerTypeEnum.MANUAL, -1, null, executorParam);
		return ReturnT.SUCCESS;
	}

	@RequestMapping("/triggerByName")
	@ResponseBody
	//@PermessionLimit(limit = false)
	public ReturnT<String> triggerByName(String name, String executorParam) {
		// force cover job param
		if (executorParam == null) {
			executorParam = "";
		}
		XxlJobInfo xxlJobInfo = xxlJobService.getByName(name);
		if(xxlJobInfo == null) {
			logger.warn("Failed to trigger scheduled task = {}", name);
			return ReturnT.FAIL;
		}
		JobTriggerPoolHelper.trigger(xxlJobInfo.getId(), TriggerTypeEnum.MANUAL, -1, null, executorParam);
		return ReturnT.SUCCESS;
	}
}
